﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Transactions;
using MuchoMusculo.BusinessObjects;

namespace MuchoMusculo.Business
{
    public partial class bsClientes
    {
        public List<tbClientes> GetListClients(string pCedula)
        {
            BDMuchoMusculosDataContext dc = new BDMuchoMusculosDataContext();
            List<tbClientes> clients = new List<tbClientes>();

            clients = (from c in dc.tbClientes
                       where c.cedula.Contains(pCedula)                 
                       select c).ToList();
            return clients;
        }

        public void AddClient(tbClientes newCliente)
        {
            BDMuchoMusculosDataContext dc = new BDMuchoMusculosDataContext();

            if (dc.tbClientes.Any(x => x.cedula == newCliente.cedula)) //El cliente ya existe.
            {
                throw new Exception("El cliente ya existe");
            }
            else
            {
                dc.tbClientes.InsertOnSubmit(newCliente);
                dc.SubmitChanges();
            }

        }

        public void EditClient(tbClientes editClient)
        {
            BDMuchoMusculosDataContext dc = new BDMuchoMusculosDataContext();
            tbClientes client = dc.tbClientes.Single(c => c.idCliente == editClient.idCliente);
            if (dc.tbClientes.Any(x => x.cedula == editClient.cedula && editClient.cedula != x.cedula))
            {
                throw new Exception("El Cliente ya se encuentra registrado en el sistema");
            }
            else
            {
                //client.apellidos = editClient.apellidos;    falta
                //client.cedula = editClient.cedula;
                //client.creadoPor = editClient.creadoPor;
                //client.direccion = editClient.direccion;
                //client.email = editClient.email;
                //client.estatura = editClient.estatura;
                //client.fechaNacimiento = editClient.fechaNacimiento;
                //client.idCliente = editClient.idCliente;
                //client.idClienteTelefono = editClient.idClienteTelefono;
                //client.idControl = editClient.idControl;
                //client.idControl = editClient.idControl;
                //client.modificadoPor = editClient.modificadoPor;
                //client.nombres = editClient.nombres;
                //client.peso = editClient.peso;
                //Probar esto -> client = editClient;

                dc.SubmitChanges();
            }
        }
    }
}
